package com.kinghood.authoritycenter.controller.oauth;


import com.kinghood.authoritycenter.common.model.dao.auth.OpenAccountDO;
import com.kinghood.authoritycenter.common.model.dto.AddOpenReqDTO;
import com.kinghood.authoritycenter.common.model.dto.auth.KingHoodPrincipal;
import com.kinghood.authoritycenter.common.model.dto.auth.UserDTO;
import com.kinghood.authoritycenter.common.model.dto.authority.CheckTokenReqDTO;
import com.kinghood.authoritycenter.common.model.dto.authority.GetUserReqDTO;
import com.kinghood.authoritycenter.common.model.dto.authority.TokenReqDTO;
import com.kinghood.authoritycenter.common.model.dto.authority.UserResDTO;
import com.kinghood.authoritycenter.manager.IOauthManager;
import com.kinghood.authoritycenter.service.IOauthService;
import com.kinghood.infrastructure.model.KingHoodResponseVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

/**
 * @author zhangFanJun
 * @date 2023-12-06 23:17
 **/
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/auth")
@RestController
public class OauthController {

    private final IOauthService oauthService;
    private final IOauthManager loginManager;

    /**
     * 系统对接获取token
     *
     * @param in 系统秘钥
     * @return token信息
     * @author zfj
     * @date 2023/12/14
     */
    @PostMapping("/openToken")
    public KingHoodResponseVO<KingHoodPrincipal<OpenAccountDO>> openToken(@RequestBody TokenReqDTO in) {

        return KingHoodResponseVO.success(loginManager.openToken(in));
    }

    /**
     * 添加系统用户
     *
     * @param in 系统秘钥
     * @return 是否成功
     * @author zfj
     * @date 2023/12/14
     */
    @PostMapping("/addOpen")
    public KingHoodResponseVO<Boolean> addOpen(@RequestBody AddOpenReqDTO in) {

        return KingHoodResponseVO.success(loginManager.addOpen(in));
    }


    /**
     * token检查
     *
     * @param in 入参
     * @return 用户信息和权限
     * @author zfj
     * @date 2023/12/26
     */
    @PostMapping("/checkToken")
    public KingHoodResponseVO<Boolean> validateToken(@RequestBody CheckTokenReqDTO in) {

        return KingHoodResponseVO.success(oauthService.validateToken(in));
    }

    /**
     * 获取用户信息和权限
     * 请求头携带token
     *
     * @return 用户信息和权限
     * @author zfj
     * @date 2023/12/26
     */
    @PostMapping("/userDetail")
    public KingHoodResponseVO<UserDTO> userDetail() {

        return KingHoodResponseVO.success(oauthService.userDetail());
    }

    /**
     * 获取用户信息和权限
     * 请求头不携带token
     *
     * @param in 入参
     * @return 用户信息和权限
     * @author zfj
     * @date 2023/12/26
     */
    @PostMapping("/getUser")
    public KingHoodResponseVO<UserResDTO> getUser(@RequestBody GetUserReqDTO in) {

        return KingHoodResponseVO.success(oauthService.getUser(in));
    }

    /**
     * 获取用户信息和权限
     * 请求头不携带token
     *
     * @param in 入参
     * @return 用户信息和权限
     * @author zfj
     * @date 2023/12/26
     */
    @PostMapping("/getUserMono")
    public Mono<KingHoodResponseVO<UserResDTO>> getUserMono(@RequestBody GetUserReqDTO in) {

        return Mono.just(KingHoodResponseVO.success(oauthService.getUser(in)));
    }
}
